package com.itheima.dao.impl;

import com.itheima.pojo.Account;
import com.itheima.dao.AccountDao01;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;


import java.sql.SQLException;
import java.util.List;

public class AccountDaoImpl01 implements AccountDao01 {

    private QueryRunner qr;//= new QueryRunner(C3P0Utils.getDataSource());

    public QueryRunner getQr() {
        return qr;
    }

    public void setQr(QueryRunner qr) {
        this.qr = qr;
    }

    public List<Account> findAll() throws SQLException {
        String sql = "select * from account";

        return qr.query(sql, new BeanListHandler<Account>(Account.class));
    }

    public void insert(Account account) throws SQLException {
        String sql = "insert into account values(null , ? , ?)";
        qr.update(sql , account.getName() , account.getMoney());
    }

    public void update(Account account) throws SQLException {
        String sql = "update account set name = ? , money = ? where id = ?";
        qr.update(sql , account.getName() , account.getMoney() , account.getId());
    }

    public void delete(int id) throws SQLException {
        String sql = "delete from account where id = ?";
        qr.update( sql , id);
    }
}
